package com.ruoyi.lm.controller;

import java.util.List;

import com.ruoyi.lm.domain.LmMessage;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.lm.service.ILmMessageService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 消息Controller
 * 
 * @author ruoyi
 * @date 2020-09-25
 */
@Controller
@RequestMapping("/lm/message")
public class LmMessageController extends BaseController
{
    private String prefix = "lm/message";

    @Autowired
    private ILmMessageService lmMessageService;

    @RequiresPermissions("lm:message:view")
    @GetMapping()
    public String message()
    {
        return prefix + "/message";
    }

    /**
     * 查询消息列表
     */
    @RequiresPermissions("lm:message:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LmMessage lmMessage)
    {
        startPage();
        List<LmMessage> list = lmMessageService.selectLmMessageList(lmMessage);
        return getDataTable(list);
    }

    /**
     * 导出消息列表
     */
    @RequiresPermissions("lm:message:export")
    @Log(title = "消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LmMessage lmMessage)
    {
        List<LmMessage> list = lmMessageService.selectLmMessageList(lmMessage);
        ExcelUtil<LmMessage> util = new ExcelUtil<LmMessage>(LmMessage.class);
        return util.exportExcel(list, "message");
    }

    /**
     * 新增消息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存消息
     */
    @RequiresPermissions("lm:message:add")
    @Log(title = "消息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LmMessage lmMessage)
    {
        lmMessage.setMessageType("系统");
        return toAjax(lmMessageService.insertLmMessage(lmMessage));
    }

    /**
     * 修改消息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        LmMessage lmMessage = lmMessageService.selectLmMessageById(id);
        mmap.put("lmMessage", lmMessage);
        return prefix + "/edit";
    }

    /**
     * 修改保存消息
     */
    @RequiresPermissions("lm:message:edit")
    @Log(title = "消息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LmMessage lmMessage)
    {
        return toAjax(lmMessageService.updateLmMessage(lmMessage));
    }

    /**
     * 删除消息
     */
    @RequiresPermissions("lm:message:remove")
    @Log(title = "消息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(lmMessageService.deleteLmMessageByIds(ids));
    }
}
